package org.darkforest.common.services.impl;

import org.darkforest.common.services.PdLogService;
import org.darkforest.dao.mapper.PdLogMapper;
import org.darkforest.model.PdLog;
import org.darkforest.model.PdLogSearchBean;
import org.darkforest.model.PdLogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/27.
 */
@Service
public class PdLogServiceimpl implements PdLogService {

    @Autowired
    private PdLogMapper pdLogMapper;

    @Override
    public int deleteByPrimaryKey(Integer lgId) {
        return pdLogMapper.deleteByPrimaryKey(lgId);
    }

    @Override
    public int insert(PdLog record) {
        return pdLogMapper.insert(record);
    }

    @Override
    public int insertSelective(PdLog record) {
        return pdLogMapper.insertSelective(record);
    }

    @Override
    public PdLog selectByPrimaryKey(Integer lgId) {
        return pdLogMapper.selectByPrimaryKey(lgId);
    }

    @Override
    public int updateByPrimaryKeySelective(PdLog record) {
        return pdLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PdLog record) {
        return pdLogMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<PdLogVo> selectByMember(PdLogSearchBean page) {
        return pdLogMapper.selectByMember(page);
    }

    @Override
    public List<PdLogVo> selectByCondition(PdLogSearchBean pdLog) {
        return pdLogMapper.selectByCondition(pdLog);
    }

    /**
     * 统计预存款根据日期
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsPdLogByDay(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsPdLogByDay(searchBean);
        return  getRegionHashMap(list);
    }

    /**
     * 统计预存款根据月份
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsPdLogByMonth(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsPdLogByMonth(searchBean);
        return  getRegionHashMap(list);
    }

    /**
     * 用户自定义时间查询统计
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsPdLogByCustom(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsPdLogByCustom(searchBean);
        return  getRegionHashMap(list);
    }

    /**
     * 查询某天的数据
     *
     * @param searchBean
     * @return
     */
    @Override
    public List<PdLogVo> selectListByDay(PdLogSearchBean searchBean) {
        return pdLogMapper.selectListByDay(searchBean);
    }

    /**
     * 按月查询
     *
     * @param searchBean
     * @return
     */
    @Override
    public List<PdLogVo> selectListByMonth(PdLogSearchBean searchBean) {
        return pdLogMapper.selectListByMonth(searchBean);
    }

    /**
     * 用户自定义日期查询
     *
     * @param searchBean
     * @return
     */
    @Override
    public List<PdLogVo> selectListByCustom(PdLogSearchBean searchBean) {
        return pdLogMapper.selectListByCustom(searchBean);
    }

    /**
     * 根据指定日期统计充值 体现 支付……总额
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsByDay(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsByDay(searchBean);
        return getRegionHashMap(list);
    }

    /**
     * 根据指定月份统计充值 体现 支付……总额
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsByCustom(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsByCustom(searchBean);
        return getRegionHashMap(list);
    }

    /**
     * 根据时间段内统计充值 体现 支付……总额
     *
     * @param searchBean
     * @return
     */
    @Override
    public Map<String, Object> statisticsByMonth(PdLogSearchBean searchBean) {
        List<Map<String,Object>> list =pdLogMapper.statisticsByMonth(searchBean);
        return getRegionHashMap(list);
    }


    /**
     * 将一些特殊的List<Map<String,Object>> 转化成Map<String,Object>
     * @param regionMap
     * @return
     */
    public static Map<String,Object> getRegionHashMap( List<Map<String,Object>> regionMap) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (Map<String, Object> map : regionMap) {
            String code = null;
            Object value = null;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if ("code_field".equals(entry.getKey())) {
                    code =  entry.getValue()+"";
                } else if ("value_field".equals(entry.getKey())) {
                    value = entry.getValue();                }
            }
            resultMap.put(code, value);
        }
        return resultMap;
    }

}
